﻿/* Copyright (c) 2011, John Cronin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysila
{
	partial class x86_64_Assembler
	{
        public class x86_64_Instruction : CodeBlock.CompiledInstruction
        {
            public class Prefixes
            {
                public bool LOCK = false;
                public bool REPNE = false;
                public bool REP = false;
                public bool OP_SIZE = false;
                public bool ADDR_SIZE = false;

                public bool REX_W = false;
                public bool REX_R = false;
                public bool REX_X = false;
                public bool REX_B = false;
            }

            public string opcode;

            public class Opcode_def
            {
                public string name;
                public byte[] opcodes;

                public enum Encoding { modRM_reg, modRM_rm, imm8, imm16, imm32, imm64, RAX, none }
                public Encoding Operand1 = Encoding.none;
                public Encoding Operand2 = Encoding.none;
                public Encoding Operand3 = Encoding.none;
                public Encoding Operand4 = Encoding.none;
            }

            public class Opcodes
            {
                public static Opcode_def AAA = new Opcode_def { name = "aaa", opcodes = new byte[] { 0x37 } };
                public static Opcode_def ADD_04 = new Opcode_def { name = "add", opcodes = new byte[] { 0x04 }, Operand1 = Opcode_def.Encoding.RAX, Operand2 = Opcode_def.Encoding.imm8 };

            }

            public Prefixes Prefix = new Prefixes();
            public Opcode_def Opcode;
            public hardware_location Operand1, Operand2, Operand3, Operand4;
            public byte Scale = 0x00;
            public hardware_location Index;
            public hardware_location Base;
            public int Displacement;
            public int Immediate;

            public override IList<byte> GetCompiledRepresentation()
            {
                throw new NotImplementedException();
            }

            /* public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                if (Prefix.LOCK)
                    sb.Append("lock ");
                if (Prefix.REPNE)
                    sb.Append("repne ");
                if (Prefix.REP)
                    sb.Append("rep ");
                if (Prefix.REX_W)
                    sb.Append("rex.w ");
                if (Prefix.REX_R)
                    sb.Append("rex.r ");
                if (Prefix.REX_X)
                    sb.Append("rex.x ");
                if (Prefix.REX_B)
                    sb.Append("rex.b ");

                sb.Append(Opcode.name);

                return sb.ToString();
            } */

            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();

                sb.Append(opcode);

                if (Operand1 != null)
                    sb.Append(" " + Operand1.ToString());
                if (Operand2 != null)
                    sb.Append(", " + Operand2.ToString());
                if (Operand3 != null)
                    sb.Append(", " + Operand3.ToString());
                if (Operand4 != null)
                    sb.Append(", " + Operand4.ToString());

                return sb.ToString();
            }
        }
	}
}
